/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica.jpaControllers;

import Entidades.Estadocontrato;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Contrato;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Detallecontrato;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class EstadocontratoJpaController {

    public EstadocontratoJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Estadocontrato estadocontrato) {
        if (estadocontrato.getContratoCollection() == null) {
            estadocontrato.setContratoCollection(new ArrayList<Contrato>());
        }
        if (estadocontrato.getDetallecontratoCollection() == null) {
            estadocontrato.setDetallecontratoCollection(new ArrayList<Detallecontrato>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Contrato> attachedContratoCollection = new ArrayList<Contrato>();
            for (Contrato contratoCollectionContratoToAttach : estadocontrato.getContratoCollection()) {
                contratoCollectionContratoToAttach = em.getReference(contratoCollectionContratoToAttach.getClass(), contratoCollectionContratoToAttach.getNumeroContrato());
                attachedContratoCollection.add(contratoCollectionContratoToAttach);
            }
            estadocontrato.setContratoCollection(attachedContratoCollection);
            Collection<Detallecontrato> attachedDetallecontratoCollection = new ArrayList<Detallecontrato>();
            for (Detallecontrato detallecontratoCollectionDetallecontratoToAttach : estadocontrato.getDetallecontratoCollection()) {
                detallecontratoCollectionDetallecontratoToAttach = em.getReference(detallecontratoCollectionDetallecontratoToAttach.getClass(), detallecontratoCollectionDetallecontratoToAttach.getIdDetalleContrato());
                attachedDetallecontratoCollection.add(detallecontratoCollectionDetallecontratoToAttach);
            }
            estadocontrato.setDetallecontratoCollection(attachedDetallecontratoCollection);
            em.persist(estadocontrato);
            for (Contrato contratoCollectionContrato : estadocontrato.getContratoCollection()) {
                Estadocontrato oldEstadocontratoOfContratoCollectionContrato = contratoCollectionContrato.getEstadocontrato();
                contratoCollectionContrato.setEstadocontrato(estadocontrato);
                contratoCollectionContrato = em.merge(contratoCollectionContrato);
                if (oldEstadocontratoOfContratoCollectionContrato != null) {
                    oldEstadocontratoOfContratoCollectionContrato.getContratoCollection().remove(contratoCollectionContrato);
                    oldEstadocontratoOfContratoCollectionContrato = em.merge(oldEstadocontratoOfContratoCollectionContrato);
                }
            }
            for (Detallecontrato detallecontratoCollectionDetallecontrato : estadocontrato.getDetallecontratoCollection()) {
                Estadocontrato oldEstadocontratoOfDetallecontratoCollectionDetallecontrato = detallecontratoCollectionDetallecontrato.getEstadocontrato();
                detallecontratoCollectionDetallecontrato.setEstadocontrato(estadocontrato);
                detallecontratoCollectionDetallecontrato = em.merge(detallecontratoCollectionDetallecontrato);
                if (oldEstadocontratoOfDetallecontratoCollectionDetallecontrato != null) {
                    oldEstadocontratoOfDetallecontratoCollectionDetallecontrato.getDetallecontratoCollection().remove(detallecontratoCollectionDetallecontrato);
                    oldEstadocontratoOfDetallecontratoCollectionDetallecontrato = em.merge(oldEstadocontratoOfDetallecontratoCollectionDetallecontrato);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Estadocontrato estadocontrato) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadocontrato persistentEstadocontrato = em.find(Estadocontrato.class, estadocontrato.getIdestadocontrato());
            Collection<Contrato> contratoCollectionOld = persistentEstadocontrato.getContratoCollection();
            Collection<Contrato> contratoCollectionNew = estadocontrato.getContratoCollection();
            Collection<Detallecontrato> detallecontratoCollectionOld = persistentEstadocontrato.getDetallecontratoCollection();
            Collection<Detallecontrato> detallecontratoCollectionNew = estadocontrato.getDetallecontratoCollection();
            List<String> illegalOrphanMessages = null;
            for (Contrato contratoCollectionOldContrato : contratoCollectionOld) {
                if (!contratoCollectionNew.contains(contratoCollectionOldContrato)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Contrato " + contratoCollectionOldContrato + " since its estadocontrato field is not nullable.");
                }
            }
            for (Detallecontrato detallecontratoCollectionOldDetallecontrato : detallecontratoCollectionOld) {
                if (!detallecontratoCollectionNew.contains(detallecontratoCollectionOldDetallecontrato)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Detallecontrato " + detallecontratoCollectionOldDetallecontrato + " since its estadocontrato field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Contrato> attachedContratoCollectionNew = new ArrayList<Contrato>();
            for (Contrato contratoCollectionNewContratoToAttach : contratoCollectionNew) {
                contratoCollectionNewContratoToAttach = em.getReference(contratoCollectionNewContratoToAttach.getClass(), contratoCollectionNewContratoToAttach.getNumeroContrato());
                attachedContratoCollectionNew.add(contratoCollectionNewContratoToAttach);
            }
            contratoCollectionNew = attachedContratoCollectionNew;
            estadocontrato.setContratoCollection(contratoCollectionNew);
            Collection<Detallecontrato> attachedDetallecontratoCollectionNew = new ArrayList<Detallecontrato>();
            for (Detallecontrato detallecontratoCollectionNewDetallecontratoToAttach : detallecontratoCollectionNew) {
                detallecontratoCollectionNewDetallecontratoToAttach = em.getReference(detallecontratoCollectionNewDetallecontratoToAttach.getClass(), detallecontratoCollectionNewDetallecontratoToAttach.getIdDetalleContrato());
                attachedDetallecontratoCollectionNew.add(detallecontratoCollectionNewDetallecontratoToAttach);
            }
            detallecontratoCollectionNew = attachedDetallecontratoCollectionNew;
            estadocontrato.setDetallecontratoCollection(detallecontratoCollectionNew);
            estadocontrato = em.merge(estadocontrato);
            for (Contrato contratoCollectionNewContrato : contratoCollectionNew) {
                if (!contratoCollectionOld.contains(contratoCollectionNewContrato)) {
                    Estadocontrato oldEstadocontratoOfContratoCollectionNewContrato = contratoCollectionNewContrato.getEstadocontrato();
                    contratoCollectionNewContrato.setEstadocontrato(estadocontrato);
                    contratoCollectionNewContrato = em.merge(contratoCollectionNewContrato);
                    if (oldEstadocontratoOfContratoCollectionNewContrato != null && !oldEstadocontratoOfContratoCollectionNewContrato.equals(estadocontrato)) {
                        oldEstadocontratoOfContratoCollectionNewContrato.getContratoCollection().remove(contratoCollectionNewContrato);
                        oldEstadocontratoOfContratoCollectionNewContrato = em.merge(oldEstadocontratoOfContratoCollectionNewContrato);
                    }
                }
            }
            for (Detallecontrato detallecontratoCollectionNewDetallecontrato : detallecontratoCollectionNew) {
                if (!detallecontratoCollectionOld.contains(detallecontratoCollectionNewDetallecontrato)) {
                    Estadocontrato oldEstadocontratoOfDetallecontratoCollectionNewDetallecontrato = detallecontratoCollectionNewDetallecontrato.getEstadocontrato();
                    detallecontratoCollectionNewDetallecontrato.setEstadocontrato(estadocontrato);
                    detallecontratoCollectionNewDetallecontrato = em.merge(detallecontratoCollectionNewDetallecontrato);
                    if (oldEstadocontratoOfDetallecontratoCollectionNewDetallecontrato != null && !oldEstadocontratoOfDetallecontratoCollectionNewDetallecontrato.equals(estadocontrato)) {
                        oldEstadocontratoOfDetallecontratoCollectionNewDetallecontrato.getDetallecontratoCollection().remove(detallecontratoCollectionNewDetallecontrato);
                        oldEstadocontratoOfDetallecontratoCollectionNewDetallecontrato = em.merge(oldEstadocontratoOfDetallecontratoCollectionNewDetallecontrato);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = estadocontrato.getIdestadocontrato();
                if (findEstadocontrato(id) == null) {
                    throw new NonexistentEntityException("The estadocontrato with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadocontrato estadocontrato;
            try {
                estadocontrato = em.getReference(Estadocontrato.class, id);
                estadocontrato.getIdestadocontrato();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The estadocontrato with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Contrato> contratoCollectionOrphanCheck = estadocontrato.getContratoCollection();
            for (Contrato contratoCollectionOrphanCheckContrato : contratoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Estadocontrato (" + estadocontrato + ") cannot be destroyed since the Contrato " + contratoCollectionOrphanCheckContrato + " in its contratoCollection field has a non-nullable estadocontrato field.");
            }
            Collection<Detallecontrato> detallecontratoCollectionOrphanCheck = estadocontrato.getDetallecontratoCollection();
            for (Detallecontrato detallecontratoCollectionOrphanCheckDetallecontrato : detallecontratoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Estadocontrato (" + estadocontrato + ") cannot be destroyed since the Detallecontrato " + detallecontratoCollectionOrphanCheckDetallecontrato + " in its detallecontratoCollection field has a non-nullable estadocontrato field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(estadocontrato);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Estadocontrato> findEstadocontratoEntities() {
        return findEstadocontratoEntities(true, -1, -1);
    }

    public List<Estadocontrato> findEstadocontratoEntities(int maxResults, int firstResult) {
        return findEstadocontratoEntities(false, maxResults, firstResult);
    }

    private List<Estadocontrato> findEstadocontratoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Estadocontrato as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Estadocontrato findEstadocontrato(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Estadocontrato.class, id);
        } finally {
            em.close();
        }
    }

    public int getEstadocontratoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Estadocontrato as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public Estadocontrato findEstadocontrato_x_nombre(String nom) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Estadocontrato u where u.nombre = " + "'" + nom + "'");
            return (Estadocontrato) q.getSingleResult();
        } finally {
            em.close();
        }
    }
}
